package huang.homework15;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
		BaseDao bd=new BaseDao();
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		
		public void close() throws SQLException{
			if(rs!=null){
				rs.close();
			}
			if(ps!=null){
				ps.close();
			}
			if(con!=null){
				con.close();
			}
		}
		public User selectUserbyName(String username) throws SQLException{
			con=bd.getCon();
			String sql="select * from user where username=?";
			ps=con.prepareStatement(sql);
			ps.setString(1, username);
			rs=ps.executeQuery();
			String name=null;
			String pwd=null;
			String sex=null;
			while(rs.next()){
				name=rs.getString("username");
				pwd=rs.getString("pwd");
				sex=rs.getString("sex");	
			}
			close();
			User user=new User(name,pwd,sex);
			return user;			
		}
		public boolean insert(User user) throws SQLException{
			boolean result=false;
			con=bd.getCon();
			String sql="insert into user (username,pwd,sex) values(?,?,?)";
			ps=con.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPwd());
			ps.setString(3, user.getSex());
			int a=ps.executeUpdate();
			if(a>0){
				result=true;
			}
			close();
			return result;
		}
}
